<template>
  <div class="chat-page">
    <div class="chat-container">
      <div class="chat-header">
        <h1>智能问数小助手</h1>
      </div>
      <div class="chat-history" ref="chatHistory">
        <div v-for="(item, index) in chatHistory" :key="index" class="chat-item">
          <div class="user-message">
            <div class="message-content">{{ item.question }}</div>
            <div class="user-avatar">
              <span>Q</span>
            </div>
          </div>
          <div class="ai-answer" v-if="item.data">
            <div class="ai-avatar">A</div>
            <div class="content">
              <div class="answer-box">
                <div class="answer-intro">根据您的问题，查询结果如下：</div>
                <div class="data-display">
                  <table>
                    <thead>
                      <tr>
                        <th v-for="column in item.columns" :key="column">{{ column }}</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr v-for="(row, rowIndex) in item.data" :key="rowIndex">
                        <td v-for="column in item.columns" :key="column">{{ row[column] }}</td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="input-area">
        <input 
          v-model="question" 
          placeholder="请输入您的问题"
          @keyup.enter="sendQuestion"
        >
        <button @click="sendQuestion">发送</button>
      </div>
    </div>

    <!-- 问题灵感 -->
    <div class="inspiration-panel">
      <h3>💡 问题灵感</h3>
      <div class="inspiration-list">
        <div 
          v-for="(item, index) in inspirations" 
          :key="index"
          class="inspiration-item"
          @click="selectInspiration(item)">
          {{ item }}
        </div>
      </div>
    </div>

    <!-- 跳转按钮 -->
    <button class="prompt-button" @click="goToPrompts">配置 Prompt</button>
  </div>
</template>

<script>
export default {
  name: 'ChatPage',
  data() {
    return {
      question: '',
      chatHistory: [],
      inspirations: [
        "24年KPI收入TOP10城市",
        "24年Q4收入大于1w的省份",
        "24年专业收入最少的5个城市",
        "本季度收入最佳产品LV3"
      ],
      multiTurnEnabled: false  // 多轮对话开关状态
    }
  },
  methods: {
    async sendQuestion() {
      if (!this.question.trim()) return
      
      const newChat = {
        question: this.question,
        answer: '正在思考中...',
        data: null,
        columns: []
      }
      this.chatHistory.push(newChat)
      
      try {
        const response = await fetch('http://127.0.0.1:5000/api/ask_number', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ question: this.question })
        })
        const data = await response.json()
        
        newChat.answer = data.answer
        if (data.data) {
          newChat.data = data.data
          newChat.columns = data.columns
        }
      } catch (error) {
        newChat.answer = '抱歉，发生了错误'
        console.error('请求失败:', error)
      }
      
      this.question = ''
    },
    selectInspiration(question) {
      this.question = question
    },
    goToPrompts() {
      this.$router.push({ name: 'Prompts' })
    }
  }
}
</script>

<style scoped>
.chat-page {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  position: relative;
}

.chat-container {
  background-color: #ffffff;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0, 40, 130, 0.08);
  overflow: hidden;
}

.chat-header {
  padding: 20px;
  background: linear-gradient(135deg, #1e88e5, #1565c0);
  color: #ffffff;
  text-align: center;
}

.chat-history {
  height: 400px;
  overflow-y: auto;
  padding: 20px;
}

.user-message {
  display: flex;
  justify-content: flex-end;
  margin: 16px 0;
}

.message-content {
  background: linear-gradient(135deg, #1e88e5, #1565c0);
  color: #ffffff;
  padding: 12px 16px;
  border-radius: 12px 12px 0 12px;
  max-width: 80%;
}

.ai-answer {
  display: flex;
  margin: 16px 0;
}

.ai-avatar {
  background: #e3f2fd;
  color: #1565c0;
  border: 1px solid #bbdefb;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.answer-box {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(30, 136, 229, 0.1);
  width: 100%;
  border: 1px solid #e3f2fd;
}

.input-area {
  display: flex;
  gap: 10px;
  padding: 20px;
  background-color: #ffffff;
  border-top: 1px solid #e3f2fd;
}

input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  padding: 10px 20px;
  background: #1976d2;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.inspiration-panel {
  margin-top: 20px;
  background-color: #ffffff;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0, 40, 130, 0.08);
  padding: 20px;
}

.inspiration-panel h3 {
  margin: 0 0 16px 0;
  color: #1565c0;
  font-size: 16px;
  font-weight: 500;
}

.inspiration-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.inspiration-item {
  padding: 12px 16px;
  background-color: #e3f2fd;
  border: 1px solid #bbdefb;
  border-radius: 8px;
  color: #1565c0;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.inspiration-item:hover {
  background-color: #bbdefb;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(21, 101, 192, 0.1);
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
}

th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

th {
  background-color: #f2f2f2;
}

tr:nth-child(even) {
  background-color: #f9f9f9;
}

.prompt-button {
  position: absolute;
  bottom: 20px;
  right: 20px;
  padding: 10px 20px;
  background: #1e88e5;
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  transition: background 0.3s, transform 0.3s;
}

.prompt-button:hover {
  background: #1565c0;
  transform: translateY(-2px);
}
</style>